<template>

  <div style="margin:40px;">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="margin:0;font-size:18px;font-weight:bold;">{{ this.$route.meta.title }}</span>
        <el-button style="float: right; padding:3px 0" type="text" @click="cancelForm">返回列表</el-button>
      </div>
      <div style="max-width:1000px;margin:auto;">
        <el-form
          ref="form"
          :model="form"
          :rules="form.houseType==0?rulesPesonal:rules"
          class="table-form"

          size="small"
        >
          <table class="table table-bordered th-bg">
            <tbody>
            <tr>
              <th width="15%">年度</th>
              <td width="35%">
                <el-form-item prop="year">
                  <el-date-picker 
                    v-model="form.year" 
                    clearable size="small" 
                    type="year" 
                    placeholder="请选择年度" 
                    value-format="yyyy" 
                  >
                  </el-date-picker>
                </el-form-item>
              </td>
              <th width="15%">区镇</th>
              <td width="35%">
                <el-form-item prop="district">
                  <el-input v-model="form.district">
                  </el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th width="15%">亩均税收<br/>（万元/亩）</th>
              <td>
                <el-form-item prop="taxPerMu" class="editor">
                  <el-input v-model="form.taxPerMu">
                  </el-input>
                </el-form-item>
              </td>
              <th width="15%">亩均销售<br/>（万元/亩）</th>
              <td>
                <el-form-item prop="salePerMu" class="editor">
                  <el-input v-model="form.salePerMu">
                  </el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th width="15%">全员劳动生产率<br/>（万元/人）</th>
              <td>
                <el-form-item prop="overallLaborProductivity" class="editor">
                  <el-input v-model="form.overallLaborProductivity">
                  </el-input>
                </el-form-item>
              </td>
              <th width="15%">研发经费占销售比重<br/>（%）</th>
              <td>
                <el-form-item prop="rdSalesRatio" class="editor">
                  <el-input v-model="form.rdSalesRatio">
                  </el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th width="15%">单位能耗增加值<br/>（万元/吨标准煤）</th>
              <td>
                <el-form-item prop="unitEnergyAdd">
                  <el-input v-model="form.unitEnergyAdd">
                  </el-input>
                </el-form-item>
              </td>
              <th width="15%">单位主要污染物增加值<br/>（万元/当量）</th>
              <td>
                <el-form-item prop="unitMajorPollutantsAdd">
                  <el-input v-model="form.unitMajorPollutantsAdd">
                  </el-input>
                </el-form-item>
              </td>
            </tr>
            </tbody>
          </table>
          <div style="display:flex; align-item: center; justify-content: center; margin-top:20px;">
            <el-button
              :loading="loading"
              type="primary"
              size="small"
              @click="submitForm"
            >
              {{isEdit ? '更新六项指标' : '新增六项指标'}} 
            </el-button>            
          </div>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import crudIndex from '@/api/business/comment/index'
import { MessageBox } from 'element-ui'
const defaultForm = {
  
}

export default {
  components: {
  },
  computed: {
  },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: JSON.parse(JSON.stringify(defaultForm)),
      rules: {
        district: [
          {
            required: true,
            message: '请输入区镇'
          }
        ],
        year: [
          {
            required: true,
            message: '请选择年度'
          }
        ],
        overallLaborProductivity: [
          {
            required: true,
            message: '请输入全员劳动生产率'
          }
        ],
        rdSalesRatio: [
          {
            required: true,
            message: '请输入研发经费占销售比重'
          }
        ],
        taxPerMu: [
          {
            required: true,
            message: '请输入亩均税收'
          }
        ],
        salePerMu: [
          {
            required: true,
            message: '请输入亩均销售'
          }
        ],
        unitEnergyAdd: [
          {
            required: true,
            message: '请输入单位能耗增加值'
          }
        ],
        unitMajorPollutantsAdd: [
          {
            required: true,
            message: '请输入单位主要污染物增加值'
          }
        ],
      },  
      loading: false,
      id: null,
    }
  },

  created() {
    if (this.isEdit) {
      this.id = this.$route.params && this.$route.params.id
      this.fetchData(this.id)
    }else {
      this.$set(this.form, 'year', localStorage.getItem('year'))
    }
  },
  computed: {
    
  },
  methods: {
    fetchData(id) {
      crudIndex.get(id).then(response => {
        this.form = Object.assign({}, this.form, response.data)
    
        // if (response.data.fireFileList) {
        //   for (let i = 0; i < response.data.fireFileList.length; i++) {
        //     const item = response.data.fireFileList[i]
        //     const file = {
        //       fileId: item.fileId,
        //       name: item.fileName,
        //       url: `business/file/download?fileId=${item.fileId}`
        //     }
        //     this.fireFileList.push(file)
        //   }
        //   this.form.fireFileIdList = this.fireFileList.map(x => x.fileId)
        // }
       
      }).catch(err => {
        console.log(err)
      })
    },
    cancelForm() {
      this.$router.push({
        path: '/business/index/' ,
        query: {
          year: localStorage.getItem('year')
        }
      })
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        MessageBox.confirm('您确认执行此操作吗？', '确认操作', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          if (this.isEdit) {
            crudIndex.edit(this.form).then(() => {
              this.$notify({
                title: '成功',
                message: '修改信息成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.cancelForm()
            }).catch(err => {
              console.log(err)
              this.loading = false
            })
          } else {
            crudIndex.add(this.form).then(() => {
              this.$notify({
                title: '成功',
                message: '提交信息成功',
                type: 'success',
                duration: 2000
              })
              this.loading = false
              this.cancelForm()
            }).catch(err => {
              console.log(err)
              this.loading = false
            })
          }
        }).catch((e) => {
          console.log(e)
        });
      })
    },
    mounted() {
      // 模拟 ajax 请求，异步渲染编辑器
      setTimeout(() => {
        this.html = '<p>模拟 Ajax 异步设置内容 HTML</p>'
      }, 1500)
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
  text-align: left;
}

.amap-wrapper {
  width: 500px;
  height: 500px;
}

::v-deep .map-dialog .el-dialog__body {
  padding: 10px 20px;
}

</style>
<style src="@wangeditor/editor/dist/css/style.css"></style>